//
//  CycleSegmentViewController.m
//  pdd
//
//  Created by dihuijun on 16/12/6.
//  Copyright © 2016年 Cycle. All rights reserved.
//

#import "CycleSegmentViewController.h"


#import "SGSegmentedControl.h"

////1.口罩
#import "CDMainMasksViewController.h"

////2.卡贴
#import "CDMainKaTieViewController.h"

////3.手表手环
#import "CDMainWatchViewController.h"

////4.眼镜
#import "CDMainGlassesViewController.h"

////5.衣服
#import "CDMainClothesViewController.h"

////6.环保
#import "CDMainEnvironmentViewController.h"

////7.平板电脑
#import "CDMainPingBanViewController.h"

@interface CycleSegmentViewController ()<UIScrollViewDelegate,UITableViewDelegate>


/**   topSView   */
@property (nonatomic, strong) SGSegmentedControlDefault *topSView;
/**   bottomView   */
@property (nonatomic, strong) SGSegmentedControlBottomView *bottomView;

/**   UITableView   */
@property (nonatomic, strong) UITableView *tableView;


@end

@implementation CycleSegmentViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
//    self.automaticallyAdjustsScrollViewInsets = NO;
   //1.口罩
    CDMainMasksViewController *masksVC = [[CDMainMasksViewController alloc]init];
//    UINavigationController *nav1 = [[UINavigationController alloc]initWithRootViewController:masksVC];
    __weak typeof(self) wself = self;
    masksVC.scrollBlock = ^(CGFloat offset){
        !wself.yoffSetBlock ?:wself.yoffSetBlock(offset);
    };
    //负值
    masksVC.mainVC = self.mainVC;
    //添加
    [self addChildViewController:masksVC];
    
    
    //2.卡贴
    CDMainKaTieViewController *katieVC = [[CDMainKaTieViewController alloc]init];
    katieVC.scrollBlock = ^(CGFloat offset){
        !wself.yoffSetBlock ?:wself.yoffSetBlock(offset);
    };
    katieVC.mainVC = self.mainVC;
    [self addChildViewController:katieVC];
    //3.手表手环
    CDMainWatchViewController *watchVC = [[CDMainWatchViewController alloc]init];
    watchVC.scrollBlock = ^(CGFloat offset){
        !wself.yoffSetBlock ?:wself.yoffSetBlock(offset);
    };
    watchVC.mainVC = self.mainVC;
    [self addChildViewController:watchVC];
    //4.眼镜
    CDMainGlassesViewController *GlassVC = [[CDMainGlassesViewController alloc]init];
    GlassVC.scrollBlock = ^(CGFloat offset){
        !wself.yoffSetBlock ?:wself.yoffSetBlock(offset);
    };
    GlassVC.mainVC = self.mainVC;
    [self addChildViewController:GlassVC];
    //5.衣服
    CDMainClothesViewController *ClothesVC = [[CDMainClothesViewController alloc]init];
    ClothesVC.scrollBlock = ^(CGFloat offset){
        !wself.yoffSetBlock ?:wself.yoffSetBlock(offset);
    };
    ClothesVC.mainVC = self.mainVC;
    [self addChildViewController:ClothesVC];
    //6.环保
    CDMainEnvironmentViewController *EnvironmentVC = [[CDMainEnvironmentViewController alloc]init];
    EnvironmentVC.scrollBlock = ^(CGFloat offset){
        !wself.yoffSetBlock ?:wself.yoffSetBlock(offset);
    };
    EnvironmentVC.mainVC = self.mainVC;
    [self addChildViewController:EnvironmentVC];
    //7.平板电脑
    CDMainPingBanViewController *PingbanVC = [[CDMainPingBanViewController alloc]init];
    PingbanVC.scrollBlock = ^(CGFloat offset){
        !wself.yoffSetBlock ?:wself.yoffSetBlock(offset);
    };
    PingbanVC.mainVC = self.mainVC;
    [self addChildViewController:PingbanVC];
    
    ///放控制器
    NSArray *childVC = @[masksVC,katieVC,watchVC,GlassVC,ClothesVC,EnvironmentVC,PingbanVC];
    ///放标题
    NSArray *titles = @[@"口罩",@"卡贴",@"眼镜",@"手表/手环",@"衣服",@"环保",@"平板电脑"];
    
    // 创建底部
//    NSLog(@"%.2f    ==== %.2f",self.view.frame.size.height,screenH);
    
    ///导航栏状态栏高度
    CGFloat navHeight     = 64;
    ///滚动式图高度
    CGFloat ScrollHeight  = 180;
    ///标题高度
    CGFloat TitleHeight   = 44;
    ///工具栏高度
    CGFloat BartoolHeight = 49;
    
    
    
    //内容部分
    self.bottomView = [[SGSegmentedControlBottomView alloc]initWithFrame:CGRectMake(0, TitleHeight, screenW, screenH)];
//   screenH -navHeight-ScrollHeight-TitleHeight-BartoolHeight
    
    _bottomView.childViewController = childVC;
    _bottomView.backgroundColor = [UIColor clearColor];
    _bottomView.delegate = self;
    [self.view addSubview:_bottomView];
    
    
    
    
    
    ///顶部标题
    __weak CycleSegmentViewController *weakSelf = self;
    
    self.topSView = [SGSegmentedControlDefault segmentedControlWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 44) childVcTitle:titles isScaleText:NO didSelectedTitleIndex:^(SGSegmentedControlDefault *segmentedControlDefault, NSInteger selectedIndex) {
        ///计算滚动的位置
        CGFloat offsetX = selectedIndex * self.view.frame.size.width;
        weakSelf.bottomView.contentOffset = CGPointMake(offsetX, 0);
        [weakSelf.bottomView showChildVCViewWithIndex:selectedIndex outsideVC:self];
    }];
    
    
    _topSView.bounces = YES;
    [self.view addSubview:_topSView];
}


#pragma mark ---- UIScrollViewDelegate
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    //计算滚动到那一页
    NSInteger index = scrollView.contentOffset.x / scrollView.frame.size.width;
    
    //1.添加子控制器
    [self.bottomView showChildVCViewWithIndex:index outsideVC:self];
    //2.把对应的标题选中
    [self.topSView changeThePositionOfTheSelectedBtnWithScrollView:scrollView];
    
    
}

-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
    
 
    
//    !self.ScrollBlock ?:self.ScrollBlock(scrollView);
}


@end
